package com.asialjim.wechat.official.message.wiki;

import com.asialjim.wechat.apiservice.constant.WeChatConstants;
import com.asialjim.wechat.message.output.WeChatOutputMessage;
import com.asialjim.wechat.official.message.output.*;
import com.asialjim.wechat.official.message.output.media.*;
import com.asialjim.wechat.official.message.wiki.dto.MsgWikiInfo;
import com.asialjim.wechat.official.message.wiki.dto.MsgWikiUrl;
import lombok.var;
import org.apache.commons.lang3.StringUtils;
import java.util.Arrays;
import java.util.Collections;
import java.util.Objects;

/**
 * <h1><em>ASIAL JIM JAVA DOC</em></h1><hr/>
 * <h2>ENUM DESCRIPTION <i>[ NAME: MsgWikiType ]</i></h2><strong>
 * <p> 微信公众号自动回复消息知识库类型
 * </strong><p><p><em>Copyright &copy; Asial Jim Co., LTD</em><hr/>
 *
 * @author Asial Jim &nbsp;&nbsp; <span>Email:<a href="mailto:asialjim@hotmail.com">asialjim@hotmail.com</a> &nbsp;&nbsp; <a href="asialjim@qq.com">asialjim@qq.com</a></span>
 * @version 1.0.0
 * @since 2021/3/23   &nbsp;&nbsp; JDK 8
 */
public enum MsgWikiType {
    text{
        @Override
        public WeChatOutputMessage outputMessage(MsgWikiInfo wikiInfo, MsgWikiUrl wikiUrl) {
            var message = new WeChatOutputTextMessage();
            message.setMsgType(WeChatConstants.OutputXmlMsgType.TEXT);
            var content = wikiInfo.getContent();
            String targetContent;
            if (Objects.nonNull(wikiUrl) && StringUtils.isNotBlank(wikiUrl.getUrl())) {
                String url = wikiUrl.getUrl();
                targetContent = "<a href=\""+ url + "\">" + content + "</a>";
            } else {
                targetContent = content;
            }
            message.setContent(targetContent);
            return message;
        }
    },
    image{
        @Override
        public WeChatOutputMessage outputMessage(MsgWikiInfo wikiInfo, MsgWikiUrl wikiUrl) {
            var message = new WeChatOutputImageMessage();
            message.setMsgType(WeChatConstants.OutputXmlMsgType.IMAGE);
            message.setImage(Image.builder().MediaId(wikiInfo.getMediaId()).build());
            return message;
        }
    },
    voice{
        @Override
        public WeChatOutputMessage outputMessage(MsgWikiInfo wikiInfo, MsgWikiUrl wikiUrl) {
            var message = new WeChatOutputVoiceMessage();
            message.setMsgType(WeChatConstants.OutputXmlMsgType.VOICE);
            var voice = Voice.builder().MediaId(wikiInfo.getMediaId()).build();
            message.setVoice(voice);
            return message;
        }
    },
    video{
        @Override
        public WeChatOutputMessage outputMessage(MsgWikiInfo wikiInfo, MsgWikiUrl wikiUrl) {
            var message = new WeChatOutputVideoMessage();
            message.setMsgType(WeChatConstants.OutputXmlMsgType.VIDEO);
            var video = Video.builder().Description(wikiInfo.getDescription())
                    .mediaId(wikiInfo.getMediaId())
                    .title(wikiInfo.getTitle())
                    .build();
            message.setVideo(video);
            return message;
        }
    },
    music{
        @Override
        public WeChatOutputMessage outputMessage(MsgWikiInfo wikiInfo, MsgWikiUrl wikiUrl) {
            var message = new WeChatOutputMusicMessage();
            message.setMsgType(WeChatConstants.OutputXmlMsgType.MUSIC);

            Music music = Music.builder().description(wikiInfo.getDescription())
                    .HQMusicUrl(wikiUrl.getHqMusicUrl())
                    .musicUrl(wikiUrl.getMusicUrl())
                    .ThumbMediaId(wikiUrl.getThumbMediaId())
                    .title(wikiInfo.getTitle())
                    .build();
            message.setMusic(music);
            return message;
        }
    },
    news{
        @Override
        public WeChatOutputMessage outputMessage(MsgWikiInfo wikiInfo, MsgWikiUrl wikiUrl) {
            Article item = Article.builder().Title(wikiInfo.getTitle())
                    .Description(wikiInfo.getDescription())
                    .PicUrl(wikiUrl.getPicUrl())
                    .Url(wikiUrl.getUrl())
                    .build();
            Articles articles = Articles.builder().item(Collections.singletonList(item)).build();

            var message = new WeChatOutputNewsMessage();
            message.setArticleCount(1);
            message.setArticles(articles);
            return message;
        }
    },
    function{
        @Override
        public WeChatOutputMessage outputMessage(MsgWikiInfo wikiInfo, MsgWikiUrl wikiUrl) {
            return null;
        }
    };

    public boolean equals(String wikiType){
        return this.name().equals(wikiType);
    }

    public static MsgWikiType nameOf(String name){
        return Arrays.stream(values()).filter(value ->value.equals(name)).findFirst().orElse(function);
    }

    public abstract WeChatOutputMessage outputMessage(MsgWikiInfo wikiInfo, MsgWikiUrl wikiUrl);
}
